﻿using System;
using System.Collections.Generic;
using EngineDrawer.Effects.ParticleSystem;
using EngineDrawer.Effects.ParticleSystem.Core;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ThinkDotNetEngine.Bots.Ammos;

namespace ThinkDotNetEngineDrawer.Bots.Ammos
{
    public class MissileEffectsAmmoDrawer:RocketParticleEffectsDrawer
    {
        private ParticleSystem _fireParticleSystem;
        private readonly Dictionary<AmmoBase, ParticleEmitter> _fireTrailParticleEmitters = new Dictionary<AmmoBase, ParticleEmitter>();

        #region Consts

        private const short FireProjectileTrailParticlesPerSec = 200;

        #endregion

        public MissileEffectsAmmoDrawer(Game game, Type supportableAmmoType) : base(game, supportableAmmoType)
        {
        }
        public override void Initialize()
        {
            base.Initialize();
            _fireParticleSystem=new FireParticleSystem(Game,Game.Content);
            Game.Components.Add(_fireParticleSystem);
        }
        public override void DrawEffects(GameTime gameTime, AmmoBase ammo)
        {
            _fireParticleSystem.SetCamera(World.ViewMatrix, World.ProjMatrix);
            const float fireTrailOffsetDelay = 20;
            _fireTrailParticleEmitters[ammo].Update(gameTime, ammo.CurrentPosition - fireTrailOffsetDelay * ammo.CurrentDirection);

            base.DrawEffects(gameTime, ammo);
        }
        protected override void RemoveAmmo(AmmoBase ammoBase)
        {
            base.RemoveAmmo(ammoBase);
            _fireTrailParticleEmitters.Remove(ammoBase);
        }
        protected override void AddNewAmmo(AmmoBase ammoBase)
        {
            base.AddNewAmmo(ammoBase);
            var particleEmitter = new ParticleEmitter(_fireParticleSystem,
                                                      FireProjectileTrailParticlesPerSec, ammoBase.CurrentPosition);
            _fireTrailParticleEmitters.Add(ammoBase, particleEmitter);
        }
    }
}
